package com.aiti.lulian.strategy.task;


import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardUserIncreaseRankDto;
import com.aiti.lulian.entity.CardRankTaskUserComplete;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.enums.RankIncreaseTaskStrategyEnum;
import com.aiti.lulian.mapper.CardRankTaskUserCompleteMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.service.CardUserRankTaskHistoryService;
import com.aiti.lulian.service.ILuLianEnterpriseService;
import com.aiti.lulian.vo.CardRankTaskUserVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class EnterpriseCardBecomeVipStrategy implements RankIncreaseTaskStrategy {

    @Autowired
    private CardRankTaskUserCompleteMapper cardRankTaskUserCompleteMapper;
    @Autowired
    private CardUserRankTaskHistoryService cardUserRankTaskHistoryService;
    @Autowired
    private CardPersonalBaseInfoMapper cardBaseInfoMapper;
    @Autowired
    private ILuLianEnterpriseService enterpriseService;

    @Override
    public RankIncreaseTaskStrategyEnum getStrategy() {
        return RankIncreaseTaskStrategyEnum.ENTERPRISE_CARD_BECOME_VIP;
    }

    @Override
    public void handleTask(CardRankTaskUserVo vo, CardUserIncreaseRankDto dto) {
        CardBaseInfo cardBaseInfo = cardBaseInfoMapper.selectById(dto.getCardId());
        String enterpriseName = cardBaseInfo.getEnterpriseName();
        if(!StringUtils.isEmpty(enterpriseName)){
            vo.setDoneFlag(true);
        }else{
            vo.setDoneFlag(false);
        }
        List<LuLianEnterprise> enterpriseList = enterpriseService.list(new LambdaQueryWrapper<LuLianEnterprise>().eq(LuLianEnterprise::getBaseName, enterpriseName).eq(LuLianEnterprise::getIsDelete, "0"));
        if(!CollectionUtils.isEmpty(enterpriseList)){
            if(enterpriseList.size() > 1){
                vo.setDoneFlag(false);
            }
            LuLianEnterprise luLianEnterprise = enterpriseList.get(0);
            Integer miniProgramVersion = luLianEnterprise.getMiniProgramVersion();
            Date expireTime = luLianEnterprise.getExpireTime();
            if(miniProgramVersion != null){
                if(expireTime == null || expireTime.compareTo(new Date()) > 0 ){
                    vo.setDoneFlag(true);
                }else{
                    vo.setDoneFlag(false);
                }
            }else{
                vo.setDoneFlag(false);
            }
        }else{
            vo.setDoneFlag(false);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message handle(CardUserIncreaseRankDto dto) {
        String userId = dto.getUserId();
        String cardId = dto.getCardId();
        List<CardRankTaskUserVo> cardRankTaskUserVos = getCompletedTask(dto);
        if(CollectionUtils.isEmpty(cardRankTaskUserVos)){
            return Message.fail("任务不存在");
        }else{
            if(cardRankTaskUserVos.get(0).isCompleteFlag()) {//已完成
                return Message.fail("该用户已完成过该任务，请勿重复");
            }else{
                CardBaseInfo cardBaseInfo = cardBaseInfoMapper.selectById(cardId);
                String enterpriseName = cardBaseInfo.getEnterpriseName();
                if(StringUtils.isEmpty(enterpriseName)){
                    return Message.fail("公司信息为空，请完善");
                }
                List<LuLianEnterprise> enterpriseList = enterpriseService.list(new LambdaQueryWrapper<LuLianEnterprise>().eq(LuLianEnterprise::getBaseName, enterpriseName).eq(LuLianEnterprise::getIsDelete, "0"));
                if(!CollectionUtils.isEmpty(enterpriseList)){
                    if(enterpriseList.size() > 1){
                        return Message.fail("系统中该公司存在多个，请联系系统管理员");
                    }
                    LuLianEnterprise luLianEnterprise = enterpriseList.get(0);
                    Integer miniProgramVersion = luLianEnterprise.getMiniProgramVersion();
                    Date expireTime = luLianEnterprise.getExpireTime();
                    if(miniProgramVersion != null){
                        if(expireTime == null || expireTime.compareTo(new Date()) > 0 ){
                            CardRankTaskUserComplete cardRankTaskUserComplete = new CardRankTaskUserComplete();
                            cardRankTaskUserComplete.setCardId(cardId);
                            cardRankTaskUserComplete.setUserId(userId);
                            cardRankTaskUserComplete.setTaskId(cardRankTaskUserVos.get(0).getBaseId());
                            cardRankTaskUserCompleteMapper.insert(cardRankTaskUserComplete);
//                LambdaQueryWrapper<CardUserRankTaskHistory> wrapper = new LambdaQueryWrapper<CardUserRankTaskHistory>().eq(CardUserRankTaskHistory::getCardId, cardId)
//                        .eq(CardUserRankTaskHistory::getType, RankIncreaseTaskStrategyEnum.REAL_NAME_AUTH.getType());
//                List<CardUserRankTaskHistory> list = cardUserRankTaskHistoryService.list(wrapper);
//                if(!CollectionUtils.isEmpty(list)){
//                    return Message.fail("该用户已领取过奖励，请勿重复领取");
//                }


                            Long score = cardRankTaskUserVos.get(0).getScore();
                            dto.setScore(score);
                            increseScore(dto);
                            recordHistory(dto);


                            return Message.success();
                        }else{
                            return Message.fail("VIP已过期，请联系管理员续费");
                        }
                    }else{
                        return Message.fail("目前未成为VIP，请先成为VIP再领取");
                    }



                }else{
                    return Message.fail("公司信息不存在，请重新完善");
                }


            }
        }
    }

    @Override
    public CardRankTaskUserCompleteMapper getCardRankTaskUserCompleteMapper() {
        return cardRankTaskUserCompleteMapper;
    }

    @Override
    public CardPersonalBaseInfoMapper getBaseInfoMapper() {
        return cardBaseInfoMapper;
    }

    @Override
    public CardUserRankTaskHistoryService getHisotryService() {
        return cardUserRankTaskHistoryService;
    }


}